package com.tech.koufu.ui.adapter;

import java.util.ArrayList;

import com.tech.koufu.R;
import com.tech.koufu.model.EntrustStock;
import com.tech.koufu.model.StockInfo;
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.ui.adapter.SimpleStockAdapter.ViewHolder;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
/**
 * 委托列表设配器
 * @author jl_luo
 *
 */
public class EntrustStocksAdapter extends BaseAdapter {
	private Context context;  
	private LayoutInflater mInflater = null; 
	public ArrayList<EntrustStock> datas;
	
	public EntrustStocksAdapter(Context context,ArrayList<EntrustStock> datas){
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
		this.datas = datas;
	}
	@Override
	public int getCount() { 
		if(null!=datas){
			return datas.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) { 
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) { 
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_entrust, parent,false);  
             holder.txt_stock_name = (TextView)convertView.findViewById(R.id.txt_stock_name);  
             holder.txt_stock_code = (TextView)convertView.findViewById(R.id.txt_stock_code);   
             holder.txt_stock_sellnum = (TextView)convertView.findViewById(R.id.txt_stock_sellnum);  
             holder.txt_stock_sellnum_value = (TextView)convertView.findViewById(R.id.txt_stock_sellnum_value);  
             
             //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag  
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         EntrustStock stockInfo = (EntrustStock) getItem(position);
         holder.txt_stock_name.setText(stockInfo.getStockName());
         holder.txt_stock_code.setText(stockInfo.getStockId());  
   	  	 holder.txt_stock_sellnum.setText(stockInfo.getStockbj());
        //总盈亏金额/成交总金额*100%
   	  	//总盈亏金额=成本价*持股数量 - 当前价* 持股数量
   	  	//成交总金额 = 当前价*卖出持股数量 
   	  	 float bj =  Float.valueOf(stockInfo.getStockbj()!=null?stockInfo.getStockbj():stockInfo.price);//成本价
//   	  	 float cyl = Float.valueOf(stockInfo.getStockHold());//持有数量
   	  	 float dqj = Float.valueOf(stockInfo.price);//当前价
//   	  	 float amount = Float.valueOf(stockInfo.getAmount());//卖出数量
   	  	// String v = KouFuTools.df2.format((bj*cyl-dqj*cyl)/(dqj*amount)*100)+"%"; 
   	  	 String v = KouFuTools.df2.format((dqj-bj)/(bj)*100)+"%"; 
        holder.txt_stock_sellnum_value .setText(v);
        
         
         return convertView;
	}

	static class ViewHolder  
    {    
        public TextView txt_stock_name;  
        public TextView txt_stock_code;   
        public TextView txt_stock_sellnum;
        public TextView txt_stock_sellnum_value;
        
    }  

	 

}
